package com.jingyanzi.channel.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.channel.dao.ChannelwindowMapper;
import com.jingyanzi.channel.domain.ChannelWindowDO;
import com.jingyanzi.channel.entity.Channelwindow;
import com.jingyanzi.channel.entity.ChannelwindowExample;
import com.jingyanzi.channel.entity.ChannelwindowExample.Criteria;
import com.jingyanzi.channel.query.ChannelWindowQueryDO;
import com.jingyanzi.channel.result.ResultDO;
import com.jingyanzi.channel.result.ResultSupport;
import com.jingyanzi.channel.service.IChannelWindowService;
import com.jingyanzi.channel.util.BeanUtilsExtends;

@Service("channelwindowservice")
public class ChannelWindowServiceImpl extends BaseServiceImpl implements IChannelWindowService {

	@Autowired
	private ChannelwindowMapper channelwindowMapper;

	@Override
	public ResultDO add(ChannelWindowDO channelWindowDO) {
		Channelwindow channelwindow = new Channelwindow();

		ResultSupport result = BeanUtilsExtends.copy(channelwindow, channelWindowDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		channelwindow.setCreatetime(Calendar.getInstance().getTime());
		channelwindow.setIsDelete(1);
		int r = -1;

		try {
			r = channelwindowMapper.insert(channelwindow);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道窗口失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道窗口失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channelwindow.getId());
		return result;
	}

	@Override
	public ResultDO modifi(ChannelWindowDO channelWindowDO) {
		Channelwindow channelwindow = new Channelwindow();

		ResultSupport result = BeanUtilsExtends.copy(channelwindow, channelWindowDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = channelwindowMapper.updateByPrimaryKeySelective(channelwindow);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改渠道窗口失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改渠道窗口失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channelwindow.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer[] ids) {
		Channelwindow channelwindow = new Channelwindow();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < ids.length; i++) {
			channelwindow.setId(ids[i]);
			channelwindow.setIsDelete(-1);
			int r = -1;

			try {
				r = channelwindowMapper.updateByPrimaryKeySelective(channelwindow);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道窗口失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道窗口失败");
				result.setSuccess(false);
				return result;
			}
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, channelwindow.getId());
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		Channelwindow channelwindow = new Channelwindow();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			channelwindow = channelwindowMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道窗口失败");
			result.setSuccess(false);
			return result;
		}

		if (channelwindow == null) {
			result.setErrorCode(ResultDO.CHANNELWINDOW_GET_ERROR);
			result.setErrorMsg("不存在该渠道窗口");
			result.setSuccess(false);
			return result;
		}
		ChannelWindowDO channelWindowDO = getChannelWindowDO(channelwindow);
		result.setModel(ResultDO.FIRST_MODEL_KEY, channelWindowDO);
		return result;
	}

	@Override
	public ResultDO query(ChannelWindowQueryDO channelWindowQueryDO) {
		ResultSupport result = new ResultSupport();

		List<Channelwindow> channelwindows = null;
		List<ChannelWindowDO> list = null;
		ChannelwindowExample example = new ChannelwindowExample();
		Criteria criterion = example.createCriteria();
		if (channelWindowQueryDO.getChannelTypeId() != null) {
			criterion.andChannelTypeIdEqualTo(channelWindowQueryDO.getChannelTypeId());
		}
		if (channelWindowQueryDO.getName() != null) {
			criterion.andWindowNameLike("%" + channelWindowQueryDO.getName() + "%");
		}
		criterion.andIsDeleteEqualTo(1);
		if (channelWindowQueryDO.isPage()) {
			example.setLimitByPage(channelWindowQueryDO.getStart() + "," + channelWindowQueryDO.getPageRows());
			long count = channelwindowMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		try {
			channelwindows = channelwindowMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道窗口失败");
			result.setSuccess(false);
			return result;
		}
		list = getChannelWindowDOList(channelwindows);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list);

		return result;
	}

}
